<?php
header('Content-type: application/json;charset=utf-8');
require_once(dirname(__FILE__) . '/inc/config.inc.php');
require_once "function.php";
IsModelPriv('goodsorder');
/*
进度：
0 已经撤单
1 报修成功
2 客服已经联系
3 系统已经派单
4 工程师接单
5 维修完成
6 已经评价
*/

//获取 post的json数据
//获取 goodsid
$op_uid = $_SESSION['uid'];

$id = htmlspecialchars($_POST['id']);
$op = htmlspecialchars($_POST['op']);


if (empty($id)) {
    exit('非法操作');
}

if (isset($id)) {

    $time = time();
    //获取工单的信息
    $sql_u = "SELECT f.* FROM `#@__order` as f	WHERE f.is_delete=0 AND f.id=$id ";
    $k = $dosql->GetOne($sql_u);
    $jindu = intval($k['jindu']);
    $typename = getRepairMode($k['apply_device']);
    $guzhang = $k['content'];
    $truename = $k['truename'];
    $mobile = $k['mobile'];
    $address = $k['school'] . "-" . $k['address'];
    $remark2 = $k['remark'];
    $task_db = $k['apply_device'];
    $mids = $k['manageid'];
    $school = $k['school'];


    $orderidname = date('Ymd', time()) . "-" . $id;
    $post_time2 = date("Y-m-d H:i:s", time());
    $apply_time = date("Y-m-d H:i:s", $k['post_time']);

    //标记为已经联系
    if ($op == 'lianxi') {
        $remark = htmlspecialchars($_POST['remark']);
        //判断是否超时
        if ($jindu = 1) {

            //修改订单状态  为 客服已经联系
            $dosql->ExecNoneQuery("update  `#@__order` set jindu='2',remark='$remark',remark_time='$time' where id='$id' ");
            //记录工单操作
            $op_name1 = '客服已经联系确认';
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,remark,manageid) values ('$id','$op_name1','$time','2','$remark',$op_uid)");
            $jssdk->return_json_body(['code' => 0, 'msg' => '操作成功']);
        } else {
            $jssdk->return_json_body(['code' => 1, 'msg' => '状态不正确，请打开重试']);
        }


    }

    // 派单
    if ($op == 'paidan') {

        $mids = htmlspecialchars($_POST['manageid']);
        $ids_arr = explode(",", $mids);

        //执行人
        $zhixingren = "";
        foreach ($ids_arr as $key => $val) {
            $zhixingren .= getManage($val) . "    ";
        }


        //判断是否超时
        if ($jindu = '2') {
            $json_data = array('msg' => 'ok');
            $beginTime = strtotime($beginTime);
            $endTime = strtotime($endTime);
            $remark = isset($remark) ? $remark : "";

            $tasktype = empty($_POST['task_type']) ? 0 : intval($_POST['task_type']);
            $task = empty($_POST['task']) ? 0 : intval($_POST['task']);


            //派单
            if ($type == 'pai') {
                //调取任务奖励
                if ($task > 0) {
                    $fee_arr = $dosql->GetOne("SELECT * FROM `#@__task` WHERE   id='$task'  ORDER BY id desc");
                    if (is_array($fee_arr) && isset($fee_arr['id'])) {
                        $fee = $fee_arr['value'];
                    }
                }
                //修改订单状态  为 已经派单
                $dosql->ExecNoneQuery("update  `#@__order` set jindu='3',manageid='$mids',begintime='$beginTime',endtime='$endTime',apply_type='$tasktype',apply_device='$task',fee='$fee' where id='$id' ");

                //写入 工单操作记录
                $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid,remark,op_manageid) values ('$id','已经派单','$time','3','$mids','$remark','$op_uid')");
                //$jssdk->return_json_body(['code' => 0, 'msg' => '派单成功']);

            }
            //改派
            if ($type == 'gai') {

                $fee = "";
                //调取任务奖励
                if ($task_db > 0) {
                    $fee_arr = $dosql->GetOne("SELECT * FROM `#@__task` WHERE   id='$task'  ORDER BY id desc");
                    if (is_array($fee_arr) && isset($fee_arr['id'])) {
                        $fee = $fee_arr['value'];
                    }
                }
                //改派操作
                $dosql->ExecNoneQuery("update  `#@__order` set jindu='3',manageid='$mids',begintime='$beginTime',endtime='$endTime' where id='$id' ");
                $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid,remark,op_manageid) values ('$id','改派执行人','$time','3','$mids','$remark','$op_uid')");

            }

            foreach ($ids_arr as $key => $val) {
                //获取工程师 openid
                $m = $dosql->GetOne("select `openid`  from `#@__user`  where id='$val'");
                $openid_str = $m['openid'];
                //记录工单操作
                if ($type == 'pai') {
                    $op_name = $_SESSION['nickname'] . '已经派单';
                } else {
                    $op_name = $_SESSION['nickname'] . '改派执行人';

                }

                $z_remark = "";
                if ($remark != "") {
                    $z_remark = "转单备注：" . $remark . "  ";
                }

                //封装为json格式的数据
                $repairname = getRepairMode($task_db);
                $repairname = $repairname;

                $send_body['touser'] = $openid_str;
                $send_body['template_id'] = $cfg_chongxinfenpei;
                $send_body['url'] = $cfg_weburl . 'wx/morderDetail.php?orderid=' . $id;
                $send_body['data']['first']['value'] = "📢 您好," . $op_name . "！单号：$orderidname";
                $send_body['data']['first']['color'] = "#FF0000";
                $send_body['data']['keyword1']['value'] = "💗" . $school . "-" . $truename;;
                $send_body['data']['keyword1']['color'] = "#173177";
                $send_body['data']['keyword2']['value'] = $repairname;
                $send_body['data']['keyword2']['color'] = "#FF0000";
                $send_body['data']['keyword3']['value'] = $apply_time;
                //$send_body['data']['keyword3']['color'] = "#FF0000";
                $send_body['data']['keyword4']['value'] = $zhixingren;
                $send_body['data']['keyword4']['color'] = "#0e48cb";
                $send_body['data']['keyword5']['value'] = $post_time2;
                $send_body['data']['keyword5']['color'] = "#0e48cb";
                $send_body['data']['remark']['value'] = $z_remark . "👉请点击处理，如有特殊情况，请转单给其他工程师。服务热线:" . $cfg_hotline . "👈";
                $send_body['data']['remark']['color'] = "#66bf45";

                $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $g_AccessToken;
                $result = $jssdk->https_curl_json($url, $send_body, 'json');

            }
            $jssdk->return_json_body(['code' => 0, 'msg' => '操作成功']);

        } else {
            $jssdk->return_json_body(['code' => 2, 'msg' => '状态不正确']);
        }

    }


// 中止任务
    if ($op == 'stop') {
        $stopremark = htmlspecialchars($_POST['stopremark']);
        //判断是否超时
        if ($jindu > 1) {
            $json_data = array('msg' => 'ok');
            //修改订单状态  为 客服已经联系
            $dosql->ExecNoneQuery("update  `#@__order` set jindu='0',isstop='1',stopremark='$stopremark',stoptime='$time' where id='$id' ");
            //记录工单操作
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,remark,manageid) values ('$id','中止任务','$time','0','$stopremark',$op_uid)");


            if ($mids != "") {
                //拉取工单中 工程师
                $m_arr = explode(",", $mids);
                //执行人
                $zhixingren = "";
                foreach ($m_arr as $key => $val) {
                    $zhixingren .= getManage($val) . "    ";
                }
                //任务类型
                $repairname = getRepairMode($task_db);

                //处理工单
                if (count($m_arr) > 0) {
                    foreach ($m_arr as $key => $val) {

                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];

                        //封装为json格式的数据

                        $send_body['touser'] = $openid_str;
                        $send_body['template_id'] = $cfg_orderstop;
                        $send_body['url'] = $cfg_weburl . 'wx/morderDetail.php?orderid=' . $id;
                        $send_body['data']['first']['value'] = "📢 您好,您提交的报修单已被暂停，请及时关注。" . $repairname . $orderidname;
                        $send_body['data']['first']['color'] = "#FF0000";
                        $send_body['data']['keyword1']['value'] = $apply_time;
                        $send_body['data']['keyword1']['color'] = "#173177";
                        $send_body['data']['keyword2']['value'] = "💗" . $school . "-" . $truename;;
                        $send_body['data']['keyword2']['color'] = "#173177";
                        $send_body['data']['keyword3']['value'] = $post_time2;
                        $send_body['data']['keyword4']['value'] = "管路一";
                        $send_body['data']['keyword4']['color'] = "#0e48cb";
                        $send_body['data']['keyword5']['value'] = $stopremark;
                        $send_body['data']['keyword5']['color'] = "#0e48cb";
                        $send_body['data']['remark']['value'] ="👉请知悉。服务热线:" . $cfg_hotline . "👈";
                        $send_body['data']['remark']['color'] = "#66bf45";

                        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $g_AccessToken;
                        $result = $jssdk->https_curl_json($url, $send_body, 'json');


                    }
                }
            }
        } else {
            $json_data = array('msg' => 'error');
        }

        $errno = array('errno' => 0);

        $json_json = array_merge($errno, $json_data);
        $data = json_encode($json_json);
        echo $data;
    }


//删除任务
    if ($op == 'delM') {
        $id = htmlspecialchars($_POST['id']);
        if (isset($id)) {
            $time = time();


            //获取工单的时间
            $k = $dosql->GetOne("select jindu from `#@__order`  where id='$id'  AND jindu>4  ");

            //判断是否超时
            if (is_array($k) && count($k) > 0) {
                $json_data = array('msg' => 'error', 'err_msg' => '已经完修任务，不能删除');
            } else {
                $dosql->ExecNoneQuery("update  `#@__order`  set is_delete='1',deltime='$time' where id='$id' ");
                //记录工单操作

                $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,remark,manageid) values ('$id','删除任务','$time','0','',$op_uid)");


                if ($mids != "") {
                    //拉取工单中 工程师
                    $m_arr = explode(",", $mids);


                    //执行人
                    $zhixingren = "";
                    foreach ($m_arr as $key => $val) {
                        $zhixingren .= getManage($val) . "    ";
                    }
                    //任务类型
                    $repairname = getRepairMode($repairid0);

                    //处理工单

                    if (count($m_arr) > 0) {
                        foreach ($m_arr as $key => $val) {

                            //获取工程师 openid
                            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                            $openid_str = $m['openid'];
                            //封装为json格式的数据
                            $json2 = '{
						  "touser": "' . $openid_str . '",  
						  "template_id": "' . $cfg_order_status . '", 
						  "data": {
							  "first": {
								  "value": "您好，任务已经删除。", 
								  "color": "#f71b03"
							  }, 
							  "keyword1": {
								  "value": "' . $orderidname . '", 
								  "color": "#173177"
							  }, 
							  "keyword2": {
								  "value": "' . $post_time2 . '", 
								  "color": "#173177"
							  },
							  "keyword3": {
								  "value": "' . $zhixingren . '", 
								  "color": "#0e48cb"
							  },
							  "keyword4": {
								  "value": " ' . $repairname . '", 
								  "color": "#173177"
							  },
							  "keyword5": {
								  "value": "任务已经删除，请知悉！", 
								  "color": "#173177"
							  },
							  "remark": {
								  "value": "服务热线：' . $cfg_hotline . '", 
								  "color": "#f71b03"
							  } 
						  }
						}';
                            sendWxMsg($json2);

                        }
                    }
                }

                $json_data = array('msg' => 'ok');
            }
        }
        $errno = array('errno' => 0);
        $json_json = array_merge($errno, $json_data);
        $data = json_encode($json_json);
        echo $data;

    }


// 确认订单
    if ($op == 'is_accept') {
        $id = htmlspecialchars($_POST['id']);
        if (isset($id)) {
            $time = time();
            //获取工单的时间
            $k = $dosql->GetOne("select * from `#@__order`  where id='$id'  AND jindu=1  ");
            //判断是否超时
            if (is_array($k)) {
                if ($k['is_accept'] == 1) {
                    $json_data = array('msg' => 'error', 'err_msg' => '已经确认，不要重复确认');
                } else {
                    $dosql->ExecNoneQuery("update  `#@__order`  set is_accept='1',accept_time='$time' where id='$id' ");
                    //记录工单操作
                    $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,remark,manageid) values ('$id','确认工单','$time','1','',$op_uid)");
                    $json_data = array('msg' => 'ok');
                }
            }
            $errno = array('errno' => 0);
            $json_json = array_merge($errno, $json_data);
            $data = json_encode($json_json);
            echo $data;
        }
    }

// 奖励操作
    if ($op == 'jiangli') {
        $remark = htmlspecialchars($_POST['remark']);
        $op_type = htmlspecialchars($_POST['op_type']);
        $fee = htmlspecialchars($_POST['fee']);
        $date = isset($_POST['date']) ?htmlspecialchars($_POST['date']) : 0 ;
        $datetime = strtotime($date);
        $feetype = isset($_POST['feetype']) ?  htmlspecialchars($_POST['feetype']) : "";
        $nowdate=date("Ymd",$datetime);
        //判断是否超时
        if ($jindu = 5) {
            if ($mids != "") {
                //拉取工单中 工程师
                $m_arr = explode(",", $mids);
                foreach ($m_arr as $key => $val) {
                    //记录工单操作
                    $remark_str = "【" . $_SESSION['nickname'] . "】" . $remark;
                    $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark,feetype,task,nowdate) values ('$id','$op_type','$datetime','5','$val','$fee','$remark_str','$feetype','$task_db','$nowdate')");
                    //$dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,remark,manageid) values ('$id','$op_name1','$time','2','$remark',$op_uid)");
                }
                $json_data = array('msg' => 'ok');
            } else {
                $json_data = array('msg' => '没有工程师');
            }
        } else {
            $json_data = array('msg' => 'error');
        }

        $errno = array('errno' => 0);

        $json_json = array_merge($errno, $json_data);
        $data = json_encode($json_json);
        echo $data;
    }
}
?>